/**
 * 
 */
package com.wsj.web.action.homeSiteNews;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.wsj.service.IHomeSiteAdBannerDescriptServ;
import com.wsj.service.IHomeSiteAdBannerServ;
import com.wsj.service.IHomeSiteNewsClassServ;
import com.wsj.service.IHomeSiteNewsServ;
import com.wsj.service.IUserManagerServ;
import com.wsj.util.exception.BusinessException;
import com.wsj.web.action.BaseAction;
import com.wsj.web.constant.homeSiteNews.HomeSiteNewsConstant;
import com.wsj.web.constant.homeSiteNews.IndexPageContent;
import com.wsj.web.subclass.displayBean.HomeSiteNews.HomeSiteIndexPageColumnBean;
import com.wsj.web.subclass.object.HomeSiteNewsDetailBean;
import com.wsj.web.vo.homeSite.HomeSiteNews;
import com.wsj.web.vo.homeSite.HomeSiteNewsClass;

/**
 * @author fengbin  createTime:2009-7-14
 *
 *  获取资讯详情
 */
public class HomeSiteNewsDetailAction extends BaseAction {
	
	
	/** 资讯接口 */
	private IHomeSiteNewsServ homeSiteNewsServImpl;
	
	/** 广告接口 */
	private IHomeSiteAdBannerServ homeSiteAdBannerServ;
	
	/** 广告描述接口 */
	private IHomeSiteAdBannerDescriptServ homeSiteAdBannerDescriptServ;
	
	/******
	 * 用户接口
	 */
	private IUserManagerServ userManagerServ;
	
	/** 资讯分类接口 */
	private IHomeSiteNewsClassServ homeSiteNewsClassServ;
	
	
	  
	public IHomeSiteNewsClassServ getHomeSiteNewsClassServ() {
		return homeSiteNewsClassServ;
	}



	public void setHomeSiteNewsClassServ(
			IHomeSiteNewsClassServ homeSiteNewsClassServ) {
		this.homeSiteNewsClassServ = homeSiteNewsClassServ;
	}



	public IHomeSiteAdBannerDescriptServ getHomeSiteAdBannerDescriptServ() {
		return homeSiteAdBannerDescriptServ;
	}



	public void setHomeSiteAdBannerDescriptServ(
			IHomeSiteAdBannerDescriptServ homeSiteAdBannerDescriptServ) {
		this.homeSiteAdBannerDescriptServ = homeSiteAdBannerDescriptServ;
	}



	public IHomeSiteAdBannerServ getHomeSiteAdBannerServ() {
		return homeSiteAdBannerServ;
	}



	public void setHomeSiteAdBannerServ(IHomeSiteAdBannerServ homeSiteAdBannerServ) {
		this.homeSiteAdBannerServ = homeSiteAdBannerServ;
	}



	public IHomeSiteNewsServ getHomeSiteNewsServImpl() {
		return homeSiteNewsServImpl;
	}



	public void setHomeSiteNewsServImpl(IHomeSiteNewsServ homeSiteNewsServImpl) {
		this.homeSiteNewsServImpl = homeSiteNewsServImpl;
	}



	public IUserManagerServ getUserManagerServ() {
		return userManagerServ;
	}



	public void setUserManagerServ(IUserManagerServ userManagerServ) {
		this.userManagerServ = userManagerServ;
	}



	/**
	 * 通过新闻的id,获取新闻详情
	 * */
	public ActionForward loadDetailById(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		String toPage ="homeSiteNewsDetail";
		String idStr = request.getParameter("id");
		
		try {
			HomeSiteNewsDetailBean homeSiteNewsDetailBean=new  HomeSiteNewsDetailBean();
			 
			int id=0;
			if(idStr!=null  && !idStr.equals("")){
				id= Integer.valueOf(idStr);
				
			}
			
			HomeSiteNews  homeSiteNews =this.getHomeSiteNewsServImpl().getHomeSiteNewsById(id);
			BeanUtils.copyProperties(homeSiteNewsDetailBean, homeSiteNews);
			
			//获取新闻的导航属性
			List<HomeSiteNewsClass> navList = this.getNavList(homeSiteNews.getHomeSiteNewsClass());
			
			//通过随机关键字,获取相关文章
			List<HomeSiteNews> interrelatedHomeSiteNewsList = this.getInterrelatedHomeSiteNewsList(homeSiteNews);
			
			//获取右边的推荐新闻,先通过当前类目去取,如果取不到,那么再从所有的类目中去取
			List<HomeSiteNews>  commendHomeSiteNewsList = this.getCommendHomeSiteNewsList(homeSiteNews.getHomeSiteNewsClass());
			
			homeSiteNewsDetailBean.setNavList(navList);
			homeSiteNewsDetailBean.setInterrelatedHomeSiteNewsList(interrelatedHomeSiteNewsList);
			homeSiteNewsDetailBean.setCommendHomeSiteNewsList(commendHomeSiteNewsList);
			
			homeSiteNewsDetailBean.setAllKeyWords(this.getAllKeyWords(homeSiteNews.getKeyword()));
			
			//详细页推荐类目  5条文本新闻 0条图片新闻 0条引导阅读新闻
			HomeSiteIndexPageColumnBean COLUMN_TJLM = this.getHomeSiteNewsServImpl().getColumnNews(IndexPageContent.DETAIL_TJLM_NAME,5,0,IndexPageContent.GUIDE_NEWS_DISPLAY_NO);
			request.setAttribute("COLUMN_TJLM", COLUMN_TJLM);
			
			//详细页推荐新闻  5条文本新闻 0条图片新闻 0条引导阅读新闻
			HomeSiteIndexPageColumnBean COLUMN_TJXW = this.getHomeSiteNewsServImpl().getColumnNews(IndexPageContent.DETAIL_TJXW_NAME,5,0,IndexPageContent.GUIDE_NEWS_DISPLAY_NO);
			request.setAttribute("COLUMN_TJXW", COLUMN_TJXW);
			
			//详细页财经新闻  5条文本新闻 0条图片新闻 0条引导阅读新闻
			HomeSiteIndexPageColumnBean COLUMN_CJXW = this.getHomeSiteNewsServImpl().getColumnNews(IndexPageContent.DETAIL_CJXW_NAME,5,0,IndexPageContent.GUIDE_NEWS_DISPLAY_NO);
			request.setAttribute("COLUMN_CJXW", COLUMN_CJXW);
			
			
			request.setAttribute("homeSiteNewsDetailBean",homeSiteNewsDetailBean);
			
		} catch (Exception e) {
			request.setAttribute("error","你查看的页面不存在");
			toPage = "error";
		}
		return mapping.findForward(toPage);
		
		
	}


	/*******
	 * 关键字列表
	 * @param keyword
	 * @return
	 */
	 private List<String[]> getAllKeyWords(String keyWords) {
		
		 List<String[]> allKeyWords=new ArrayList<String[]>();
		 try {
			if(keyWords!=null  && !keyWords.equals("")){
				 
				 String[] keys = null;
				 
				 if(keyWords.indexOf(",")!=-1){
					 keys = keyWords.split(",");
				 }else if(keyWords.indexOf(" ")!=-1){
					 keys = keyWords.split(" ");
				 }
				 
				 if(keys!=null){
					 for(int j=0;j<keys.length;j++){
						 if(keys[j]!=null  && !keys[j].equals("")){
							 String[] dd = new String[2];
							 dd[0]=keys[j];
						     dd[1]=java.net.URLEncoder.encode(keys[j],"UTF-8");
							 allKeyWords.add(dd);
						 }
					 }
				 }
				 
			 }
			
		} catch (Exception e) {
			this.getLogger().error("getAllKeyWords",e);
		}
		 
		return allKeyWords;
	}



	/*************
	  * 获取右边的推荐新闻,先通过当前类目去取,如果取不到,那么再从所有的类目中去取
	  * @param homeSiteNewsClass
	  * @return
	 * @throws BusinessException 
	  */
     private List<HomeSiteNews> getCommendHomeSiteNewsList(HomeSiteNewsClass homeSiteNewsClass) throws BusinessException {
		
    	 return this.homeSiteNewsServImpl.getCommendHomeSiteNewsListByClassId(homeSiteNewsClass.getId(),HomeSiteNewsConstant.TOP5);
    	 
	}



	/************
      * 通过随机获取的关键字,获取5条相关文章
      * @param homeSiteNews
      * @return
	 * @throws BusinessException 
      */
	 private List<HomeSiteNews> getInterrelatedHomeSiteNewsList(HomeSiteNews homeSiteNews) throws BusinessException {
		  
		 String keyWords=homeSiteNews.getKeyword();
		 String key="";
		 if(keyWords!=null  && !keyWords.equals("")){
			 
			 String[] keys = null;
			 
			 if(keyWords.indexOf(",")!=-1){
				 keys = keyWords.split(",");
			 }else if(keyWords.indexOf(" ")!=-1){
				 keys = keyWords.split(" ");
			 }
			
			 if(keys!=null){
				 if(keys.length>1){
					 key = this.getRandomKey(keys);	 
				 }else{
					 key = keys[0];
				 }
			 }
		 }
		 
		 return this.homeSiteNewsServImpl.getInterrelatedHomeSiteNewsList(key,HomeSiteNewsConstant.TOP5);
	}



	private String getRandomKey(String[] keys) {
		
		java.util.Random r=new java.util.Random();
		 int j=r.nextInt(keys.length-1);
		 if(keys[j]!=null && !keys[j].trim().equals("")){
			 return keys[j].trim();
		 }else{
			 return this.getRandomKey(keys);
		 }
	}



	/***********
	  * 获取导航的列表 
	  * @param homeSiteNewsClass
	  * @return
	 * @throws BusinessException 
	 * @throws NumberFormatException 
	  */
	private List<HomeSiteNewsClass> getNavList(HomeSiteNewsClass homeSiteNewsClass) throws NumberFormatException, BusinessException {
		
		List<HomeSiteNewsClass> retList = new ArrayList<HomeSiteNewsClass>();
		  String seq=homeSiteNewsClass.getSeq();
		  if(seq!=null  && !seq.equals("")){
			  String[] classIdS = seq.split(",");
			  if(classIdS!=null){
				  for(int j=0;j<classIdS.length;j++){
					   if(classIdS[j]!=null  && !classIdS[j].equals("")){
						   HomeSiteNewsClass  navHomeSiteNewsClass =this.homeSiteNewsClassServ.loadNewsClassById(Long.valueOf(classIdS[j]));
						   if(navHomeSiteNewsClass!=null){
							   retList.add(navHomeSiteNewsClass);
						   }
					   }
					  
				  }
			  }
		  }
		  
		  return retList;
	}

}
